import request from '@/utils/request'

/**
 * 通用增删改查
 * @param query
 * @param module
 * @returns {AxiosPromise}
 */
export function shoucang(data) {
  return request({
    url: '/api/devfile/collet',
    method: 'post',
    data: data
  })
}

export function pasteToDir(data) {
  return request({
    url: '/api/device/paste',
    method: 'post',
    data: data
  })
}
export function buildCode(data) {
  return request({
    url: '/api/station/buildcode',
    method: 'post',
    data: data
  })
}
export function getShareUrl(params) {
  return request({
    url: '/api/share/set',
    method: 'get',
    params: params
  })
}
export function getver() {
  return request({
    url: '/api/app/getver',
    method: 'get',
  })
}

/**
 * 获取文件公用模板
 * @param data
 * @returns {AxiosPromise}
 */
export function getPublicTpl(data) {
  return request({
    url: '/api/station/get_template_id',
    method: 'get',
    params:data
  })
}
export function getEmpData(station_id) {
  return request({
    url: '/api/epm/getdata',
    // url:'/api/test/epm',
    method: 'get',
    params:{station_id:station_id?station_id:0}
  })
}
export function getSpmData(inc_type) {
  let url =  '/api/spmmis/getlink';
  if(inc_type=='mis')
  {
    url="/api/mislink/getdata";
  }
  return request({
    url: url,
    method: 'get',
    params:{inc_type:inc_type}
  })
}


export function getShareFile(params) {
  return request({
    url: '/api/share/getdata',
    method: 'get',
    params: params
  })
}

export function getReport(params) {
  return request({
    url: '/api/report/getlist',
    method: 'get',
    params: params
  })
}

export function copyTpm(id) {
  return request({
    url: '/api/template/copy',
    method: 'get',
    params: { id: id }
  })
}
export function getHistory(file_id) {
  return request({
    url: '/api/devfile/ver',
    method: 'get',
    params: { file_id }
  })
}
export function getSearch(data) {
  return request({
    url: '/api/station/gettemplatequerylist',
    method: 'post',
    data
  })
}

export function delFile(query) {
  return request({
    url: '/api/devfile/del',
    method: 'get',
    params: query
  })
}
export function viewBlobFile(query) {
  return request({
    url: '/api/devfile/down',
    method: 'get',
    responseType: 'blob',
    params: query
  })
}

export function downBlobFile(query, fileName) {
  return request({
    url: '/api/devfile/down',
    method: 'get',
    responseType: 'blob',
    params: query
  }).then((response) => {
    // console.log('下载',response);

    handleBlobFile(response, fileName)
  }).catch((e) => {
    console.log('下载出错', e)
  })
}
export function handleBlobFile(response, fileName) {
  // 处理返回的文件流
  const blob = response
  if (blob && blob.size === 0) {
    // useMessage().error('内容为空，无法下载');
    return
  }
  const link = document.createElement('a')

  // 兼容一下 入参不是 File Blob 类型情况
  var binaryData = []
  binaryData.push(response)
  link.href = window.URL.createObjectURL(new Blob(binaryData))
  link.download = fileName
  document.body.appendChild(link)
  link.click()
  window.setTimeout(function() {
    // @ts-ignore
    URL.revokeObjectURL(blob)
    document.body.removeChild(link)
  }, 0)
}
export function fetchList(query, module) {
  return request({
    url: '/api/' + module + '/getlist',
    method: 'post',
    data: query
  })
}
export function fetchOne(params, module) {
  return request({
    url: '/api/' + module + '/getone',
    method: 'get',
    params: params
  })
}
export function fetchTree(query, url) {
  return request({
    url: url,
    method: 'post',
    data: query
  })
}
export function fetchDict(name) {
  const listQuery = {
    page_no: 1,
    limit: 100,
    sort: 'id',
    where: { and: [{ field: 'name', operator: '=', value: name }] }
  }
  return request({
    url: '/api/sl_valdict/getlist',
    method: 'post',
    data: listQuery
  })
}

export function createData(data, module) {
  return request({
    url: '/api/' + module + '/save',
    method: 'post',
    data
  })
}
export function delData(data, module) {
  return request({
    url: '/api/' + module + '/del',
    method: 'post',
    data
  })
}
export function delTpl(id){
  return request({
        url: '/api/template/del_node',
        method: 'post',
        data:{id}
      })

}
export function updateData(data, module) {
  return request({
    url: '/api/' + module + '/save',
    method: 'post',
    data
  })
}
export function updateFile(data) {
  return request({
    url: '/api/devfile/upload',
    method: 'post',
    data
  })
}
export function saveTempLateRole(data, module) {
  return request({
    url: '/api/' + module + '/save',
    method: 'post',
    data
  })
}
